/*
 * 原版接口
 */
import * as server from "@minecraft/server";

/*
 * 蛋白石数据
 */
import * as data_format from "../data/format";
import * as data_entity from "../data/entity";
import * as data_story from "../data/story";
/*
 * 蛋白石组件
 */
import * as power_property from "../system/power_property";
import * as derived_tools from "../system/derived_tools";
import * as native_tools from "../system/native_tools";
import * as achieve_plan from "../system/achieve_plan";
import * as math_tools from "../system/math_tools";

/**
 * * 领航者 默认攻击 事件
 * 
 * @param {server.Entity} entity - 实体对象
 * 
 * @param {server.Entity} target - 目标实体对象
 * 
 * @param {boolean} erupt - 是否暴击
 * 
 * @param {data_format.GET_PROPERTY_PANEL} property - 属性对象
 */
export function DefaultAttack(entity: server.Entity, target: server.Entity, erupt: boolean, property: data_format.GET_PROPERTY_PANEL) {
    /**
     * * 当前生命值
     */
    const health = entity.getComponent('health');
    /**
     * * 合并属性
     */
    const mergeData = power_property.MergeProperty(property, { basic_attack: property.basic_attack * 1.5 });
    // 判断实体是否存活
    if (!health) return;
    // 判断生命值是否低于 50%
    switch (native_tools.HealthBelow(health, 0.5)) {
        //* 当前生命值低于 50%
        case true: AttackAfter(entity, target, erupt, mergeData); break;
        //* 当前生命值高于 50%
        default: AttackAfter(entity, target, erupt, property); break;
    };
};

/**
 * * 绯红 攻击事件
 * 
 * @param {server.Entity} entity - 实体对象
 * 
 * @param {server.Entity} target - 目标实体对象
 * 
 * @param {boolean} erupt - 是否暴击
 * 
 * @param {data_format.GET_PROPERTY_PANEL} property - 属性对象
 */
export function Crimson(entity: server.Entity, target: server.Entity, erupt: boolean, property: data_format.GET_PROPERTY_PANEL) {
    /**
     * * 当前生命值
     */
    const selfHealth = entity.getComponent('health');
    /**
     * * 合并属性
     */
    const mergeData = power_property.MergeProperty(property, { basic_attack: property.basic_attack * 0.5 });
    // 判断实体是否存活
    if (!selfHealth) return;
    // 暴击率增加 20%
    mergeData.raise_erupt_odds += 20;
    // 为 队友 添加 治疗效果
    native_tools.GetPartner(entity,
        entity => {
            /**
             * * 基础治疗量
             */
            const value = Math.floor(property.erupt_odds * 0.5);
            /**
             * * 当前生命值
             */
            const health = entity.getComponent('health');
            // 恢复生命值
            health?.setCurrentValue(health.currentValue + value);
            // 赋予 伤害吸收 效果
            if (native_tools.HealthBelow(selfHealth, 0.32)) entity.addEffect('absorption', 640, { amplifier: 9, showParticles: false });
        }
    );
    // 执行 攻击事件 后处理
    AttackAfter(entity, target, erupt, mergeData);
};

/**
 * * 森涅 攻击事件
 * 
 * @param {server.Entity} entity - 实体对象
 * 
 * @param {server.Entity} target - 目标实体对象
 * 
 * @param {data_format.GET_PROPERTY_PANEL} property - 属性对象
 */
export function SenNie(entity: server.Entity, target: server.Entity, property: data_format.GET_PROPERTY_PANEL) {
    /**
     * * 技能能量值
     */
    const energy = entity.getDynamicProperty('power_energy:SenNie') as number ?? 0;
    /**
     * * 伤害提升值
     */
    const advance = entity.getDynamicProperty('power_advance:SenNie') as number ?? 0;
    /**
     * * 合并属性
     */
    const mergeData = power_property.MergeProperty(property, { basic_attack: property.erupt_hurt * 0.35, double_damage: property.erupt_odds / 10 });
    /**
     * * 充能值
     */
    const charge = energy + math_tools.Random({ min: 1, max: 3 }, true);
    // 显示充能等级
    derived_tools.NumberParticleDisplay(entity, charge, math_tools.Vector.CONSTANT_UP);
    // 追加 伤害提升值
    property.damage_increase += advance;
    // 普通攻击
    if (energy < 15) {
        entity.setDynamicProperty('power_energy:SenNie', charge);
        AttackAfter(entity, target, false, property);
    }
    // 强化攻击
    else {
        entity.setDynamicProperty('power_advance:SenNie', ((property.erupt_hurt * 0.35) * (property.erupt_odds / 10)) * 0.15);
        native_tools.TrySpawnParticle(entity.dimension, 'constant:pulse_rune_green', entity.getHeadLocation());
        entity.setDynamicProperty('power_energy:SenNie', 0);
        AttackAfter(entity, target, false, mergeData);
    };
};

/**
 * * 星砂 攻击事件
 * 
 * @param {server.Entity} entity - 实体对象
 * 
 * @param {server.Entity} target - 目标实体对象
 * 
 * @param {boolean} erupt - 是否暴击
 * 
 * @param {data_format.GET_PROPERTY_PANEL} property - 属性对象
 */
export function StarSand(entity: server.Entity, target: server.Entity, erupt: boolean, property: data_format.GET_PROPERTY_PANEL) {
    /**
     * * 技能能量值
     */
    const energy = entity.getDynamicProperty('power_energy:StarSand') as number ?? 0;
    /**
     * * 合并属性
     */
    const mergeData = power_property.MergeProperty(property, { basic_attack: property.basic_attack * 0.5 });
    // 判断是否触发 暴击攻击
    if (erupt) {
        /**
         * * 合并属性 - 暴击后 额外提升
         */
        const mergeDataErupt = power_property.MergeProperty(mergeData, { raise_basic_attack: property.erupt_hurt * 0.5, raise_erupt_hurt: energy * 200 });
        // 执行 攻击事件 后处理
        native_tools.TrySpawnParticle(entity.dimension, 'constant:pulse_rune_red', entity.getHeadLocation());
        entity.setDynamicProperty('power_energy:StarSand', 0);
        AttackAfter(entity, target, erupt, mergeDataErupt);
    }
    else {
        if (energy < 5) entity.setDynamicProperty('power_energy:StarSand', energy + 1);
        target.addEffect('slowness', 80, { amplifier: 32, showParticles: false });
        AttackAfter(entity, target, erupt, mergeData);
    };
    // 显示充能等级
    derived_tools.NumberParticleDisplay(entity, energy, math_tools.Vector.CONSTANT_UP);
};

/**
 * * 月华 攻击事件
 * 
 * @param {server.Entity} entity - 实体对象
 * 
 * @param {server.Entity} target - 目标实体对象
 * 
 * @param {boolean} erupt - 是否暴击
 * 
 * @param {data_format.GET_PROPERTY_PANEL} property - 属性对象
 */
export function Moonlight(entity: server.Entity, target: server.Entity, erupt: boolean, property: data_format.GET_PROPERTY_PANEL) {
    /**
     * * 生命值组件
     */
    const health = entity.getComponent('health');
    /**
     * * 定义 粒子参数
     */
    const molang = new server.MolangVariableMap();
    if (!health) return;
    // 触发 增幅效果
    if (native_tools.HealthBelow(health, 0.1)) power_property.AlterProperty(entity, { raise_basic_attack: property.erupt_hurt * 0.6 });
    power_property.AlterProperty(entity, { raise_basic_attack: property.erupt_odds * 2 });
    /**
     * * 合并属性
     */
    const mergeData = power_property.MergeProperty(power_property.GetProperty(entity), { basic_attack: property.basic_attack * 0.5 });
    // 创建 符文伤害
    achieve_plan.RunePowerAttack.Create(entity.id, 10, { target, self: entity, erupt, hurt_data: mergeData })
    // 生命值低于 10% 时 增加 生命值 否则 减少 生命值
    if (native_tools.HealthBelow(health, 0.1)) health?.setCurrentValue(health.currentValue + 50);
    else health?.setCurrentValue(Math.floor(health.currentValue * 0.85));
    // 创建战斗奖励
    CreateReward(entity, target);
    // 设置 粒子参数
    molang.setColorRGB('variable.color', data_story.getRuneColor(mergeData.self_rune));
    molang.setVector3('variable.direction', math_tools.Vector.CONSTANT_DOWN);
    molang.setFloat('variable.range', 5);
    molang.setFloat('variable.type', 0);
    // 播放 粒子效果
    native_tools.TrySpawnParticle(entity.dimension, 'scripts:path_spurt', math_tools.Vector.add(target.location, { x: 0, y: 4, z: 0 }), molang);
    // 调用 珍珠水母 的 治疗效果
    if (derived_tools.IsEnable(10)) jellyfishInPearl(entity, property);
};

/**
 * * 琉璃 攻击事件
 * 
 * @param {server.Entity} entity - 实体对象
 * 
 * @param {server.Entity} target - 目标实体对象
 * 
 * @param {boolean} erupt - 是否暴击
 * 
 * @param {data_format.GET_PROPERTY_PANEL} property - 属性对象
 */
export function Crystal(entity: server.Entity, target: server.Entity, erupt: boolean, property: data_format.GET_PROPERTY_PANEL) {
    /**
     * * 生命值组件
     */
    const health = entity.getComponent('health');
    /**
     * * 技能能量值
     */
    const energy = entity.getDynamicProperty('power_energy:Crystal') as number ?? 0;
    /**
     * * 定义 坐标基准点
     */
    const vertex0 = math_tools.Vector.add(entity.location, { x: 8, y: 8, z: 8 });
    /**
     * * 定义 坐标基准点
     */
    const vertex1 = math_tools.Vector.add(entity.location, { x: -8, y: 0, z: -8 });
    if (!health) return;
    // 普通攻击
    if (energy < 8) {
        // 暴击时 获得 额外充能
        if (erupt) {
            /**
             * * 充能值
             */
            const charge = energy + math_tools.Random({ min: 1, max: 8 }, true);
            entity.setDynamicProperty('power_energy:Crystal', charge);
            // 显示充能等级
            derived_tools.NumberParticleDisplay(entity, charge, math_tools.Vector.CONSTANT_UP);
        }
        else {
            /**
             * * 充能值
             */
            const charge = energy + 1;
            entity.setDynamicProperty('power_energy:Crystal', charge);
            // 显示充能等级
            derived_tools.NumberParticleDisplay(entity, charge, math_tools.Vector.CONSTANT_UP);
        };
        // 执行 攻击事件 后处理
        AttackAfter(entity, target, erupt, property);
    }
    // 强化攻击
    else {
        /**
         * * 强化攻击次数
         */
        const amount = Math.floor(health.currentValue / 10);
        /**
         * * 实体查询选项
         */
        const options: server.EntityQueryOptions = {
            families: ['monster'],
            location: target.location,
            maxDistance: 4,
            closest: 8,
        };
        /**
         * * 契约者标识符
         */
        const contract = entity.getDynamicProperty('entity:contract_user') as string;
        /**
         * * 获取实体列表
         */
        const entitys = entity.dimension.getEntities(options);
        /**
         * * 合并属性
         */
        const mergeData = power_property.MergeProperty(property, { basic_attack: property.erupt_odds * amount * 0.5 });
        /**
         * * 创建 粒子效果
         */
        const animation = () => {
            /**
             * * 随机坐标
             */
            const current = math_tools.Vector.random(vertex0, vertex1);
            /**
             * * 定义 粒子参数
             */
            const molang = new server.MolangVariableMap();
            /**
             * * 获取 坐标差值
             */
            const difference = math_tools.Vector.difference(current, target.getHeadLocation());
            // 设置 粒子参数
            molang.setColorRGB('variable.color', data_story.getRuneColor(property.self_rune));
            molang.setVector3('variable.direction', difference);
            molang.setFloat('variable.range', 15);
            molang.setFloat('variable.type', 0);
            // 显示 粒子效果
            native_tools.TrySpawnParticle(entity.dimension, 'constant:the_cracks_of_the_misty_sea', current);
            native_tools.TrySpawnParticle(entity.dimension, 'scripts:path_spurt', current, molang);
        };
        // 过滤 契约者 与 锁定的目标
        entitys.filter(entity => entity.id !== contract && entity.id !== target?.id);
        // 执行粒子效果
        for (let index = 0; index < amount; index++) animation();
        // 造成 范围伤害
        server.system.runTimeout(() => entitys.forEach(entity => achieve_plan.RunePowerAttack.BriefCreate(entity, entity, erupt, mergeData)), 20);
        // 对目标单独结算一次伤害
        server.system.runTimeout(() => achieve_plan.RunePowerAttack.BriefCreate(entity, target, erupt, mergeData), 20);
        // 清空 充能值
        entity.setDynamicProperty('power_energy:Crystal', 0);
        // 创建战斗奖励
        CreateReward(entity, target);
    };
};

/**
 * * 蔷薇 攻击事件
 * 
 * @param {server.Entity} entity - 实体对象
 * 
 * @param {data_format.GET_PROPERTY_PANEL} property - 属性对象
 */
export function Rambler(entity: server.Entity, property: data_format.GET_PROPERTY_PANEL) {
    /**
     * * 生命值组件
     */
    const health = entity.getComponent('health');
    if (health && native_tools.HealthHigher(health, 0.2)) health?.setCurrentValue(Math.floor(health.currentValue * 0.5));
    // 修改属性
    native_tools.GetPartner(entity,
        entity => {
            /**
             * * 获取属性
             */
            const getData = power_property.GetProperty(entity);
            if (getData.double_damage >= 7.5) return;
            // 通用增益效果
            power_property.AlterProperty(entity, { double_damage: property.erupt_hurt * 0.015, damage_increase: property.basic_attack })
            native_tools.TrySpawnParticle(entity.dimension, 'constant:smoke_rune_purple', entity.getHeadLocation());
        }
    );
    // 提升战斗经验
    ExperienceSpecialImprove(entity);
    // 创建战斗奖励
    CreateReward(entity, entity.target ?? entity);
};

/**
 * * 海灵 攻击事件
 * 
 * @param {server.Entity} entity - 实体对象
 * 
 * @param {server.Entity} target - 目标实体对象
 * 
 * @param {boolean} erupt - 是否暴击
 * 
 * @param {data_format.GET_PROPERTY_PANEL} property - 属性对象
 */
export function Hailing(entity: server.Entity, target: server.Entity, erupt: boolean, property: data_format.GET_PROPERTY_PANEL) {
    /**
     * * 技能能量值
     */
    const energy = target.getDynamicProperty('power_energy:Ocean') as number ?? 0;
    // 提供 生命恢复 伤害吸收 效果
    native_tools.GetPartner(entity,
        entity => {
            /**
             * * 当前生命值
             */
            const health = entity.getComponent('health');
            // 恢复生命值
            health?.setCurrentValue(health.currentValue + Math.floor(property.erupt_odds * 0.5));
            entity.addEffect('absorption', 320, { amplifier: energy, showParticles: false });
        }
    );
    // 叠加印记等级
    if (energy < 5) target.setDynamicProperty('power_energy:Ocean', energy + 1);
    AttackAfter(entity, target, erupt, property);
};

/**
 * * 海娜 攻击事件
 * 
 * @param {server.Entity} entity - 实体对象
 * 
 * @param {server.Entity} target - 目标实体对象
 * 
 * @param {boolean} erupt - 是否暴击
 * 
 * @param {data_format.GET_PROPERTY_PANEL} property - 属性对象
 */
export function Haina(entity: server.Entity, target: server.Entity, erupt: boolean, property: data_format.GET_PROPERTY_PANEL) {
    /**
     * * 技能能量值
     */
    const energy = target.getDynamicProperty('power_energy:Ocean') as number ?? 0;
    /**
     * * 阶段一能量值
     */
    const stage = math_tools.Clamp({ max: 9, min: 1 }, energy);
    /**
     * * 生命值组件
     */
    const health = entity.getComponent('health');
    if (!health) return;
    /**
     * * 损伤的血量
     */
    const trauma = (health?.currentValue * 0.1) * stage;
    // 修改当生命值
    health.setCurrentValue(health.currentValue - trauma);
    // 攻击面板增加
    property.raise_erupt_odds += stage * 75;
    property.raise_basic_attack += trauma;
    // 叠加印记等级
    if (energy < 15) target.setDynamicProperty('power_energy:Ocean', energy + 1);
    AttackAfter(entity, target, erupt, property);
};

/**
 * * 幽蓝 攻击事件
 * 
 * @param {server.Entity} entity - 实体对象
 * 
 * @param {server.Entity} target - 目标实体对象
 * 
 * @param {boolean} erupt - 是否暴击
 * 
 * @param {data_format.GET_PROPERTY_PANEL} property - 属性对象
 */
export function Dullblue(entity: server.Entity, target: server.Entity, erupt: boolean, property: data_format.GET_PROPERTY_PANEL) {
    /**
     * * 过滤器参数
     */
    const options: server.EntityQueryOptions = {
        families: ['monster'],
        location: target.location,
        maxDistance: 8,
        closest: 4
    };
    /**
     * * 契约者标识符
     */
    const contract = entity.getDynamicProperty('entity:contract_user') as string;
    /**
     * * 获取实体列表
     */
    const entitys = target.dimension.getEntities(options);
    /**
     * * -1 ~ 1的随机数
     */
    const random = () => math_tools.Random({ min: -1, max: 1 }, true);
    // 造成的伤害 降低 50%
    property.double_damage *= 0.5;
    // 过滤 契约者 与 锁定的目标
    entitys.filter(entity => entity.id !== contract && entity.id !== target?.id);
    // 为队友提供暴击增益
    native_tools.GetPartner(entity, entity => power_property.AlterProperty(entity, { raise_erupt_odds: entitys.length * 15 }));
    // 对目标造成击飞伤害
    entitys.forEach(
        entity => {
            try {
                entity.applyKnockback(random(), random(), random(), Math.abs(random() * entitys.length));
            }
            catch {
                native_tools.TrySpawnParticle(entity.dimension, 'constant:pulse_rune_green', entity.location);
            };
            achieve_plan.RunePowerAttack.BriefCreate(entity, entity, erupt, property);
        }
    );
    // 对目标单独结算一次伤害
    achieve_plan.RunePowerAttack.BriefCreate(entity, target, erupt, property);
    // 创建 粒子效果
    native_tools.TrySpawnParticle(target.dimension, 'constant:impact_rune_green', target.location);
    native_tools.TrySpawnParticle(target.dimension, 'constant:excite_rune_green', target.location);
    native_tools.TrySpawnParticle(target.dimension, 'constant:erupt_rune_green', target.location);
    // 创建战斗奖励
    CreateReward(entity, target);
};

/**
 * * 九九 攻击事件
 * 
 * @param {server.Entity} entity - 实体对象
 * 
 * @param {server.Entity} target - 目标实体对象
 * 
 * @param {boolean} erupt - 是否暴击
 * 
 * @param {data_format.GET_PROPERTY_PANEL} property - 属性对象
 */
export function NineNine(entity: server.Entity, target: server.Entity, erupt: boolean, property: data_format.GET_PROPERTY_PANEL) {
    /**
     * * 计算随机值
     */
    const random = math_tools.Random({ max: 2, min: 0 }, true);
    /**
     * * 获取实体头部位置
     */
    const location = entity.getHeadLocation();
    /**
     * * 获取实体朝向向量
     */
    const point = entity.getViewDirection();
    /**
     * * 锚点偏移坐标组
     */
    const anchors = [
        { above: 1, front: 0, right: 0 },
        { above: 0, front: 0, right: 1 },
        { above: 0, front: 0, right: -1 }
    ];
    /**
     * * 炮击坐标生成函数
     * 
     * @param {number} amount - 炮击数量
     * 
     * @returns {server.Vector3[]} - 炮击坐标数组
     */
    const PhaseCannon = (amount: number): server.Vector3[] => {
        /**
         * * 输出坐标数组
         */
        const output: server.Vector3[] = [];
        // 循环生成炮击坐标
        for (let index = 0; index < amount; index++) {
            // 获取随机属性值
            switch (random) {
                case 0:
                    property.self_rune = "rune_red";
                    output.push(math_tools.Vector.relativeOffset(location, point, anchors[0]));
                    break;

                case 1:
                    property.self_rune = "rune_blue";
                    output.push(math_tools.Vector.relativeOffset(location, point, anchors[1]));
                    break;

                default:
                    property.self_rune = "rune_green";
                    output.push(math_tools.Vector.relativeOffset(location, point, anchors[2]));
                    break;
            }
        }
        return output;
    };
    // 创建 指定次数 的 炮击伤害
    PhaseCannon(erupt ? 3 : 1).forEach(
        (anchor, index) => {
            /**
             * * 容器参数
             */
            const args: achieve_plan.ROUTE_ARGS = {
                location_group: [anchor, target.getHeadLocation()],
                particles: ['constant:smoke_' + property.self_rune],
                on_done: AttackBomb(entity, erupt, property),
                dimension: entity.dimension,
                cooldown: 1,
                speed: 1
            };
            // 创建 路径容器
            server.system.runTimeout(() => achieve_plan.PathExecute.Create('神恩领航者-九九-炮击轨迹', 1, args), (1 + index) * 10);
        }
    )
    // 创建战斗奖励
    CreateReward(entity, target);
};

/**
 * * 雪隐 攻击事件
 * 
 * @param {server.Entity} entity - 实体对象
 * 
 * @param {server.Entity} target - 目标实体对象
 * 
 * @param {boolean} erupt - 是否暴击
 * 
 * @param {data_format.GET_PROPERTY_PANEL} property - 属性对象
 */
export function SnowHidden(entity: server.Entity, target: server.Entity, erupt: boolean, property: data_format.GET_PROPERTY_PANEL) {
    /**
     * * 获取目标生命值组件
     */
    const targetHealth = target.getComponent('health');
    if (!targetHealth) return;
    /**
     * * 技能阈值
     */
    const threshold = property.erupt_hurt * 2;
    /**
     * * 原始充能值
     */
    const proto = property.erupt_hurt - targetHealth.currentValue;
    /**
     * * 充能钳位值
     */
    const clamp = Math.floor(math_tools.Clamp({ max: threshold, min: 1 }, proto));
    /**
     * * 技能能量值
     */
    const energy = entity.getDynamicProperty('power_energy:SnowHidden') as number ?? clamp;
    // 释放法术攻击
    AttackAfter(entity, target, erupt, property);
    // 检测是否释放充能技能
    if (energy >= threshold) {
        /**
         * * 当前生命值
         */
        const roleHealth = entity.getComponent('health');
        // 遍历队友队列
        native_tools.GetPartner(entity,
            entity => {
                if (!roleHealth) return;
                // 恢复生命值
                entity.addEffect('absorption', (energy - roleHealth?.currentValue) * 20, { amplifier: property.basic_attack * 0.5, showParticles: false });
                entity.addEffect('health_boost', (roleHealth?.currentValue) * 20, { amplifier: property.basic_attack * 0.5, showParticles: false });
                server.world.playSound('conduit.attack', entity.location);
            }
        );
        entity.setDynamicProperty('power_energy:SnowHidden', 1);
        return;
    }
    else entity.setDynamicProperty('power_energy:SnowHidden', math_tools.Clamp({ max: threshold, min: 1 }, energy + clamp));
    // 显示充能值
    derived_tools.NumberParticleDisplay(entity, energy, math_tools.Vector.CONSTANT_UP);
};

/**
 * * 珍珠 攻击事件
 * 
 * @param {server.Entity} entity - 实体对象
 * 
 * @param {server.Entity} target - 目标实体对象
 */
export function Pearl(entity: server.Entity, target: server.Entity) {
    /**
     * * 全部动态属性ID
     */
    const propertyID = entity.getDynamicPropertyIds();
    // 判断是否可以生成水母实体
    if (achieve_plan.ControlEventTrigger.EntityToken('珍珠:水母治疗', entity, 600)) {
        /**
         * * 水母实体
         */
        const jellyfish = native_tools.TrySpawnEntity(entity.dimension, 'entity_elemental:jellyfish_of_pearl', entity.getHeadLocation());
        // 复制动态属性
        if (jellyfish instanceof Error) return;
        propertyID.forEach(id => jellyfish.setDynamicProperty(id, entity.getDynamicProperty(id)));
        jellyfish.setDynamicProperty('entity:unlock', true);
    }
    else {
        /**
         * * 游鱼实体
         */
        const fish = native_tools.TrySpawnEntity(entity.dimension, 'entity_elemental:fish_of_pearl', entity.getHeadLocation());
        // 检测是否生成游鱼实体
        if (fish instanceof Error) return;
        // 复制动态属性
        propertyID.forEach(id => fish.setDynamicProperty(id, entity.getDynamicProperty(id)));
        fish.setDynamicProperty('entity:unlock', true);
    };
    // 重置 自身属性
    power_property.SetProperty(entity, data_entity.reset_battle_data);
    // 提升战斗经验
    ExperienceSpecialImprove(entity);
    // 创建战斗奖励
    CreateReward(entity, target);
};

/**
 * * 珍珠游鱼 攻击事件
 * 
 * @param {server.Entity} entity - 实体对象
 * 
 * @param {server.Entity} target - 目标实体对象
 * 
 * @param {boolean} erupt - 是否暴击
 */
export function fishInPearl(entity: server.Entity, target: server.Entity, erupt: boolean) {
    // 创建 符文伤害
    achieve_plan.RunePowerAttack.BriefCreate(entity, target, erupt);
    /**
     * * 获取当前同类计划的数量
     */
    const tickDelay = achieve_plan.Control.inventory.filter(item => item.className === entity.typeId).length;
    // 在一段时间后移除实体
    server.system.runTimeout(() => { if (entity && entity.isValid()) entity?.remove() }, tickDelay);
    // 添加 隐身效果
    entity.addEffect('invisibility', 200, { showParticles: false });
    // 播放 音效
    server.world.playMusic('mob.slime.small');
};

/**
 * * 珍珠水母 攻击事件
 * 
 * @param {server.Entity} entity - 实体对象
 * 
 * @param {data_format.GET_PROPERTY_PANEL} property - 属性对象
 */
export function jellyfishInPearl(entity: server.Entity, property: data_format.GET_PROPERTY_PANEL) {
    native_tools.GetPartner(entity,
        entity => {
            /**
             * * 当前生命值
             */
            const health = entity.getComponent('health');
            // 恢复生命值
            health?.setCurrentValue(health.currentValue + Math.floor(property.basic_attack * 0.5));
            entity.addEffect('health_boost', 320, { amplifier: 9, showParticles: false });
        }
    );
};

/**
 * * 琥珀 攻击事件
 * 
 * @param {server.Entity} entity - 实体对象
 * 
 * @param {server.Entity} target - 目标实体对象
 */
export function Amber(entity: server.Entity, target: server.Entity) {
    /**
     * * 定义 坐标基准点
     */
    const vertex0 = math_tools.Vector.add(entity.location, { x: 8, y: 2, z: 8 });
    /**
     * * 定义 坐标基准点
     */
    const vertex1 = math_tools.Vector.add(entity.location, { x: -8, y: 0, z: -8 });
    // 在 目标位置 生成 烛火花
    native_tools.TrySpawnEntity(target.dimension, 'entity_elemental:routine_candlelight_flower', math_tools.Vector.random(vertex0, vertex1));
    native_tools.TrySpawnEntity(target.dimension, 'entity_elemental:super_candlelight_flower', math_tools.Vector.random(vertex0, vertex1));
};

/**
 * * 攻击事件 后处理
 * 
 * @param {server.Entity} entity - 实体对象
 * 
 * @param {server.Entity} target - 目标实体对象
 * 
 * @param {data_format.GET_PROPERTY_PANEL} property - 实体属性对象
 */
function AttackAfter(entity: server.Entity, target: server.Entity, erupt: boolean, property: data_format.GET_PROPERTY_PANEL, query?: server.EntityQueryOptions) {
    // 创建路径
    achieve_plan.PathExecute.Create('神恩领航者-通用攻击射线', 1,
        {
            location_group: [entity.getHeadLocation(), target.getHeadLocation()],
            on_done: AttackBomb(entity, erupt, property, query),
            particles: ['constant:track_' + property.self_rune],
            dimension: entity.dimension,
            cooldown: 1,
            speed: 1
        }
    );
    // 创建战斗奖励
    CreateReward(entity, target);
};

/**
 * * 法术攻击 命中后 的 爆炸逻辑
 * 
 * @param {server.Entity} entity - 发动法术攻击的攻击者
 * 
 * @param {erupt} erupt - 本次攻击是否暴击
 * 
 * @param {data_format.GET_PROPERTY_PANEL} property - 攻击时使用的属性面板
 * 
 * @param {server.EntityQueryOptions} query - 进行调整的实体查询参数
 * 
 */
function AttackBomb(entity: server.Entity, erupt: boolean, property: data_format.GET_PROPERTY_PANEL, query?: server.EntityQueryOptions) {
    return (args: data_format.ROUTE_ANNEX_ARGS) => {
        // 验证 实体状态 是否正确
        if (!entity || !entity.isValid()) return;
        /**
         * * 过滤器参数
         */
        const options: server.EntityQueryOptions = {
            families: ['monster'],
            location: args.location,
            maxDistance: 4,
            closest: 4,
            ...query ?? {}
        };
        /**
         * * 契约者标识符
         */
        const contract = entity.getDynamicProperty('entity:contract_user') as string;
        /**
         * * 领航者瞄准的目标
         */
        const target = entity.target;
        /**
         * * 获取实体列表
         */
        const entitys = args.dimension.getEntities(options);
        // 判断 目标 是否存在`
        if (!target || !target.isValid()) return;
        // 过滤 契约者 与 锁定的目标
        entitys.filter(entity => entity.id !== contract && entity.id !== target?.id);
        // 创建 元素伤害
        entitys.forEach(entity => achieve_plan.RunePowerAttack.BriefCreate(entity, entity, erupt, property));
        // 创建 粒子效果
        native_tools.TrySpawnParticle(args.dimension, 'constant:excite_' + property.self_rune, args.location);
        // 对目标单独结算一次伤害
        achieve_plan.RunePowerAttack.BriefCreate(entity, target, erupt, property);
    }
};

/**
 * * 创建 战斗奖励
 * 
 * @param {server.Entity} object - 实体对象
 * 
 * @param {server.Entity} target - 目标实体对象
 */
function CreateReward(object: server.Entity, target: server.Entity) {
    /**
     * * 获取 战斗经验值
     */
    const experience = object.getDynamicProperty('entity:experience') as number ?? 0;
    /**
     * * 获取 战术等级
     */
    const improve = object.getDynamicProperty('entity:improve') as number ?? 1;
    /**
     * * 创建 物品对象
     */
    const item = new server.ItemStack('item_prop:enlightenment');
    // 判断 是否发放 参悟之石
    if (experience / data_entity.experience_improve < improve) return;
    if (improve > data_entity.max_experience) return;
    // 修改 等级记录
    object.setDynamicProperty('entity:improve', improve + 1);
    native_tools.TrySpawnItem(object.dimension, item, target.location);
};

function ExperienceSpecialImprove(object: server.Entity) {
    /**
     * * 获取 战斗经验值
     */
    const experience = object.getDynamicProperty('entity:experience') as number ?? 0;
    // 提升战斗经验值
    object.setDynamicProperty('entity:experience', experience + 1);
}